#include "dialogaudience.h"

DialogAudience::DialogAudience(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);

	QTextCodec *codec = QTextCodec::codecForName("CP1251");			// podkluchaem russkuyu kodirovku
    QTextCodec::setCodecForCStrings(codec);

	ui.table->resizeColumnsToContents();							// razmer stolbcov v normu

	ui.table->setEditTriggers(QAbstractItemView::NoEditTriggers);   // zapreschaem redaktirovanie tablici

	ui.table->setSelectionMode(QAbstractItemView::SingleSelection);	//stavim maks. kol-vo strok na videleni=1

	sunday_background();

	ui.groupMenu->setEnabled(false);

	ui.groupDate->setEnabled(false);

	ui.group_aud->setEnabled(false);

	ui.table->setEnabled(false);


/*****************************************************************************************************************/	
/*------------------------------------------------    CONNECTS   ------------------------------------------------*/
/*****************************************************************************************************************/

	connect(ui.comboAud,	SIGNAL(currentIndexChanged(int)),	this, SLOT(audChanged()));
	connect(ui.table,		SIGNAL(cellClicked(int, int)),		this, SLOT(backgroundItem()));
	connect(ui.spinYears,	SIGNAL(valueChanged(int)),			this, SLOT(yearChanged()));
	connect(ui.comboMonths,	SIGNAL(currentIndexChanged(int)),	this, SLOT(monthChanged()));
	connect(ui.btnAdd,		SIGNAL(clicked()),					this, SLOT(addAud()));
	connect(ui.btnSave,		SIGNAL(clicked()),					this, SLOT(saveAud()));
	connect(ui.checkMod,	SIGNAL(toggled (bool)),				this, SLOT(modChanged()));

	
}

DialogAudience::~DialogAudience()
{

}

/*****************************************************************************************************************/
/*-------------------------------------------------    SLOTS    -------------------------------------------------*/
/*****************************************************************************************************************/


void DialogAudience::audChanged()
{
	if (ui.comboAud->currentIndex() != 0)
	{
		ui.groupMenu->setEnabled(true);

		ui.groupDate->setEnabled(true);

		ui.group_aud->setEnabled(true);

		ui.table->setEnabled(true);
	}
	else 
	{
		ui.groupMenu->setEnabled(false);

		ui.groupDate->setEnabled(false);

		ui.group_aud->setEnabled(false);

		ui.table->setEnabled(false);

	}
}

void DialogAudience::yearChanged()
{

}

void DialogAudience::monthChanged()
{

}

void DialogAudience::backgroundItem()
{	
	daysInMonth();

	dayOfWeek = tempDate.dayOfWeek();				// den nedeli vibrannoi dati
	
	if (dayOfWeek!=7)								// esli vibrannii den ne voskresenie
		if (ui.table->currentRow() != -1)			// esli ne snyato videlenie
		{
			time = ui.table->currentRow();			// zapominaem nomer stroki
			QTableWidgetItem *sItem;
			sItem = new QTableWidgetItem();
			sItem->setBackground(Qt::green);		// zakrashivaem yache'ku
			ui.table->setItem(time, day, sItem);	
		}

}

void DialogAudience::saveAud()
{

}

void DialogAudience::addAud()
{

}

void DialogAudience::modChanged()
{

}



/*******************************************************************************************************************/
/*------------------------------------------------    FUNCTIONS    ------------------------------------------------*/
/*******************************************************************************************************************/

void DialogAudience::defaultWidgets()
{

}

void DialogAudience::sunday_background()
{
	daysInMonth();

	for (int i=1; i<=countDays; i++)
	{
		tempDate.setDate(year, month, i);
		dayOfWeek = tempDate.dayOfWeek();				
		if (dayOfWeek==7)
			{
				for (int j=1; j<9; j++)
				{
					QTableWidgetItem *sItem;
					sItem = new QTableWidgetItem();
					sItem->setBackground(Qt::darkRed);		// zakrashivaem yache'ku
					ui.table->setItem(j, i-1, sItem);
				}
			}
	}
	
	//if (ui.table->currentRow() != -1)			// esli ne snyato videlenie
	//	{
			time = ui.table->currentRow();			// zapominaem nomer stroki
			QTableWidgetItem *sItem;
			sItem = new QTableWidgetItem();
			sItem->setBackground(Qt::green);		// zakrashivaem yache'ku
			ui.table->setItem(time, day, sItem);	
	//	}
}

void DialogAudience::daysInMonth()
{
	year = ui.spinYears->value();					// zapominaem ustanovlennii god	
	month = ui.comboMonths->currentIndex()+1;		// zapominaem index vibrannogo mesyaca
	day = ui.table->currentColumn();				// nomer stolbca(data)
	tempDate.setDate(year, month, day+1);			// ustanavlivaem datu
	countDays = tempDate.daysInMonth();				// uznaem kol-vo dnei v ustanovlennom mesyace
}
